﻿//se creaza namespace-ul main
Ext.ns('Portal.main');

//se creaza namespace-ul events
Ext.ns('Portal.events');
//creare seturi functii mesaje
Ext.apply(Portal.events, {
    throwError: function(msg) {
        Portal.statusBar.setStatus({
            text: msg,
            iconCls: 'x-status-error'
        });
    },

    throwInformation: function(msg) {
        Portal.statusBar.setStatus({
            text: msg,
            iconCls: 'x-status-valid'  ,
            autoClear : 10000
        });
    },
    throwWarning: function(msg) {
        Portal.statusBar.setStatus({
            text: msg,
            iconCls: 'x-status-warning'
        });
    }
});

//creare seturi functii de suprascriu functiile AJAX a.i. daca sa se afiseze mesaje ( confirmari , erori) pe interfata utilizatorului )
Ext.apply(Portal.events, {

    beforeRequest: function(conn, response, options) {
        Portal.statusBar.showBusy({
            text:'Interogare server in curs...',
            iconCls: 'x-status-busy'
        });
    },

    requestException: function(conn, response, options) {
        Portal.statusBar.clearStatus();
        Portal.events.parseJsonResponse(conn, response, options, true);
    },

    requestComplete: function(conn, response, options) {
        Portal.statusBar.clearStatus();
        Portal.events.parseJsonResponse(conn, response, options);
    },

    parseJsonResponse: function(conn, response, options, exception) {
        // case there is an exception throw an error
        try {
            if (exception !== undefined) {
                Portal.events.throwError(response.statusText);
            }
        }
        catch(e) {
        }

        try {
            // daca raspunsul este json clasic
            if ((   response.responseText.search(/\{"error":/i) !== -1    ) || (    response.responseText.search(/\{'error':/i) !== -1  )) {
                //console.info('DO DECODE');
                var oResponse = Ext.decode(response.responseText);

                if (oResponse.error !== undefined) {
                    if (( oResponse.error === true  ) || ( oResponse.error == 'true'  )) {
                        Portal.events.throwError(oResponse.description);
                        return;
                    }
                    else if ((oResponse.error === false  ) || (oResponse.error == 'false')) {
                        if (oResponse.type != 'silent') { // silent response, do not echo
                            if (oResponse.description) {
                                Portal.events.throwInformation(oResponse.description);
                            }
                            return;
                        }
                    }
                }
            }
            else {
            }
        }
        catch(er) {
            //console.error(e);
        }
    }
});

// adaugare functii pentru preluare mesaje ajax
Ext.Ajax.on('beforerequest', Portal.events.beforeRequest, this);
Ext.Ajax.on('requestcomplete', Portal.events.requestComplete, this);
Ext.Ajax.on('requestexception', Portal.events.requestException, this);


Ext.apply(Portal.main, {
    //functie de schimabre parola
    ChangePass: function () {
        Ext.MessageBox.prompt('Schimbare Parola', 'Parola :',
                function (btn, text) {
                    if (btn == 'ok') {
                        Ext.Ajax.request({
                            url: 'Users/ChangePass',
                            params: {
                                pass: text
                            }
                        });
                    }
                });
    },

    exec: function () {


        //creare status bar
        var oMainStatusBar = new Ext.ux.StatusBar({
            defaultText: '',
            busyText: 'Se incarca...',
            statusAlign: 'left'
        });
        // se face global statusbar
        Portal.statusBar = oMainStatusBar;

        //definire regiune sud care va contine statusbar
        var oSouthRegion = {
            region: 'south',
            layout: 'fit',
            collapsible: false,
            bbar: oMainStatusBar
        };


        //definire regiune `north`
        var oNorthRegion = {
            region: 'north',
            tbar: [
                {
                    text: 'Utilizatori',
                    iconCls: 'icon-group',
                    hidden: (Portal.UserRole == "admin") ? false : true,
                    handler: Portal.userScreen.exec
                },
                {
                    text: 'Contacte',
                    iconCls: 'icon-fugue-users ',
                    hidden: (Portal.UserRole == "admin") ? false : true,
                    handler: Portal.contactScreen.exec
                },
                {
                    text: 'Companii',
                    iconCls: 'icon-cog',
                    hidden: (Portal.UserRole == "admin") ? false : true,
                    handler: Portal.companyScreen.exec
                },
                {
                    text: 'Operatii',
                    iconCls: 'icon-fugue-block',
                    tooltip: 'Mici formulare pentru a adauga orase, judete, functii, domenii s.a.m.d',
                    menu: new Ext.menu.Menu({
                        id: 'menuObjects',
                        style: {
                            overflow: 'visible' // For the Combo popup
                        }
                    })
                },
                {
                    text: 'Modifica parola',
                    iconCls: 'icon-cool-exchange ',
                    handler:  Portal.main.ChangePass

                },
                {
                    text: 'Mailer',
                    iconCls: 'icon-fugue-users ',
                    hidden: (Portal.UserRole == "admin") ? false : true,
                    handler: Portal.mailerScreen.exec
                },
                {
                    text: 'Telemarketing',
                    iconCls: 'icon-telephone',
                    menu: new Ext.menu.Menu({
                        items: [
                            {
                                iconCls: 'icon-newspaper',
                                text: 'Status',
                                handler: Portal.teleStatusScreen.exec
                            },
                            {
                                iconCls: 'icon-fugue-question-frame',
                                text: 'Intrebari',
                                handler: Portal.teleQuestionScreen.exec
                            },
                            {
                                iconCls: 'icon-fugue-question-frame',
                                text: 'Intrebari cu raspuns liber',
                                handler: Portal.teleQuestionFreeScreen.exec
                            },                            
                            {
                                iconCls: 'icon-telephone',
                                text: 'Telemarketing',
                                handler: Portal.teleScreen.exec
                            }
                        ]
                    })
                },
                '->',
                {
                    text: 'Logout',
                    iconCls: 'icon-cool-box-out ',
                    handler: function () {
                        window.location = 'Login/DoLogout';
                    }
                }
            ]
        };
        //definire regiune `center`
        var oCenterRegion = {
            region: 'center',
            xtype: 'tabpanel',
            activeItem: 0,
            enableTabScroll: true,
            resizeTabs: true,
            minTabWidth: 100,
            hideMode: "offsets",
            deferredRender: false,
            defaults: {
                closable: true
            },
            items: [
                {
                    closable: true,
                    html: "User Raport ..work in progress",
                    title: "Welcome"
                }
            ]
        };

        var oAppViewport = new Ext.Viewport({
            layout: 'border',
            items: [oNorthRegion, oCenterRegion, oSouthRegion],
            renderTo: Ext.getBody()
        });

        //atribuire obiecte pentru o refolosire rapida
        Portal.oNorthRegion = oAppViewport.getComponent(0);
        Portal.oCenterRegion = oAppViewport.getComponent(1);

    }
});

Ext.onReady(Portal.main.exec, Portal.main);
//Ext.onReady(Portal.userScreen.exec, Portal.userScreen);
Ext.onReady(Portal.menuObject.exec, Portal.menuObject);
Ext.onReady(Portal.teleScreen.exec, Portal.teleScreen);